package org.threebitsoftware.tbb.module.simpletext;

import java.io.*;

import javax.swing.JPanel;

import org.threebitsoftware.tbb.dao.Address;
import org.threebitsoftware.tbb.dao.Contact;
import org.threebitsoftware.tbb.dao.EmailAddress;
import org.threebitsoftware.tbb.dao.PhoneNumber;
import org.threebitsoftware.tbb.module.fileio.FileOutputInterface;

public class SimpleTextExporter implements FileOutputInterface {

	@Override
	public boolean canExportMultipleContacts() {
		// Multiple contacts are supported
		return true;
	}

	@Override
	public boolean exportContactToFile(String myStrFilePath, Contact myContact) throws Exception {
		Contact [] myContacts = new Contact[1];
		
		myContacts[0] = myContact;
		
		return exportContactToFile(myStrFilePath, myContacts);
	}

	@Override
	public boolean exportContactToFile(String myStrFilePath,
			Contact[] myContacts) throws Exception {
		
		boolean result = false;
		BufferedWriter out = null;
		
		try {

			out = new BufferedWriter(new FileWriter(myStrFilePath));
			
			int numberOfContacts = myContacts.length;
			
			if (numberOfContacts > 0) {
				out.write("Number of Contacts:" + numberOfContacts + "\r\n");
			}
			
			int contactIndex = 1;
			
			// Write the contacts out to the file
			for (Contact contact : myContacts) {

				out.write("Contact Begin\r\n");
				
				// Write out the formatted name information
				String prefixStr = contact.getPrefix();
				String firstNameStr = contact.getFirstName();
				String middleNameStr = contact.getMiddleName();
				String lastNameStr = contact.getLastName();
				String suffixStr = contact.getSuffix();
				String companyStr = contact.getCompany();
				String titleStr = contact.getTitle();
				String isPersonStr = String.valueOf(contact.isPerson());

				if ((!prefixStr.isEmpty()) ||
					(!firstNameStr.isEmpty()) ||
					(!middleNameStr.isEmpty()) ||
					(!lastNameStr.isEmpty()) ||
					(!suffixStr.isEmpty()) ||
					(!companyStr.isEmpty()) ||
					(!titleStr.isEmpty())) {
					
					out.write("Contact Name:");
				
					if (!prefixStr.isEmpty()) {
						out.write("   Prefix=" + prefixStr);
					}
					
					if (!firstNameStr.isEmpty()) {
						out.write("   First=" + firstNameStr);
					}
					
					if (!middleNameStr.isEmpty()) {
						out.write("   Middle=" + middleNameStr);
					}
					
					if (!lastNameStr.isEmpty()) {
						out.write("   Last=" + lastNameStr);
					}
					
					if (!suffixStr.isEmpty()) {
						out.write("   Suffix=" + suffixStr);
					}
					
					if (!titleStr.isEmpty()) {
						out.write("   Title=" + titleStr);
					}
					
					if (!companyStr.isEmpty()) {
						out.write("   Company=" + companyStr);
					}
					
					out.write("   Is Person=" + isPersonStr);
					out.write("\r\n");
				}

				// Write out the User Application ID and Contact ID
				if ((contact.getIdAppUser() != -1) ||
					(contact.getIdContact() != -1)) {
					
					String idAppUserStr = String.valueOf(contact.getIdAppUser());
					String idContactStr = String.valueOf(contact.getIdContact());
					
					if ((!idAppUserStr.isEmpty()) ||
						(!idContactStr.isEmpty())) {
						
						out.write("Identification:");
						
						if (!idAppUserStr.isEmpty()) {
							out.write("   AUID=" + idAppUserStr);
						}
						
						if (!idContactStr.isEmpty()) {
							out.write("   CID=" + idContactStr);
						}
						
						out.write("\r\n");
					}
				}
				
				// Write out the addresses contained in this contact
				for (Address address : contact.getAddresses()) {
					
					String addressDescStr = address.getDescription();
					String street1Str = address.getStreet1();
					String street2Str = address.getStreet2();
					String street3Str = address.getStreet3();
					String cityStr = address.getCity();
					String provinceStr = address.getProvince();
					String countryStr = address.getCountry();
					String zipCodeStr = address.getZipCode();
					int idContact = address.getIdContact();
					int idAddress = address.getIdAddress();
					
					if ((!addressDescStr.isEmpty()) ||
						(!street1Str.isEmpty()) ||
						(!street2Str.isEmpty()) ||
						(!street3Str.isEmpty()) ||
						(!cityStr.isEmpty()) ||
						(!provinceStr.isEmpty()) ||
						(!countryStr.isEmpty()) ||
						(!zipCodeStr.isEmpty())) {

						out.write("Address:");
						
						if (!addressDescStr.isEmpty()) {
							out.write("   Description=" + addressDescStr);
						}
						
						if (!street1Str.isEmpty()) {
							out.write("   Street1=" + street1Str);
						}
						
						if (!street2Str.isEmpty()) {
							out.write("   Street2=" + street2Str);
						}
						
						if (!street3Str.isEmpty()) {
							out.write("   Street3=" + street3Str);
						}
						
						if (!cityStr.isEmpty()) {
							out.write("   City=" + cityStr);
						}
						
						if (!provinceStr.isEmpty()) {
							out.write("   Province=" + provinceStr);
						}
						
						if (!countryStr.isEmpty()) {
							out.write("   Country=" + countryStr);
						}
						
						if (!zipCodeStr.isEmpty()) {
							out.write("   Zip Code=" + zipCodeStr);
						}
						
						out.write("   CID=" + idContact);
						out.write("   AID=" + idAddress);
						
						out.write("\r\n");
					}
				}
				
				for (PhoneNumber phoneNumber : contact.getPhoneNumbers()) {
					
					String phoneNumberDescStr = phoneNumber.getDescription();
					String phoneNumberStr = phoneNumber.getPhoneNumber();
					int idPhoneNumber = phoneNumber.getIdPhoneNumber();
					int idContact = phoneNumber.getIdContact();
					
					if ((!phoneNumberDescStr.isEmpty()) ||
						(!phoneNumberStr.isEmpty())) {
						
						out.write("Phone Number:");
						
						if (!phoneNumberDescStr.isEmpty()) {
							out.write("   Description=" + phoneNumberDescStr);
						}
						
						if (!phoneNumberStr.isEmpty()) {
							out.write("   Number=" + phoneNumberStr);
						}
						
						out.write("   PID=" + idPhoneNumber);
						out.write("   CID=" + idContact);
						out.write("\r\n");
					}
				}
				
				for (EmailAddress email : contact.getEmails()) {
					
					String emailDescStr = email.getDescription();
					String emailStr = email.getEmailAddress();
					int idAppUser = email.getIdAppUser();
					int idContact = email.getIdContact();
					
					if ((!emailDescStr.isEmpty()) ||
						(!emailStr.isEmpty())) {
						
						out.write("E-mail Address:");
						
						if (!emailDescStr.isEmpty()) {
							out.write("   Description=" + emailDescStr);
						}
						
						if (!emailStr.isEmpty()) {
							out.write("   E-mail=" + emailStr);
						}
						
						out.write("   AUID=" + idAppUser);
						out.write("   CID=" + idContact);
						out.write("\r\n");
					}
				}
				
				out.write("Contact End");
				out.write("\r\n");
				contactIndex++;
				
			} // End of for (Contact contact : myContacts) loop
	        
	        out.close();
	        result = true;
	        
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
		
		return result;
	}

	@Override
	public JPanel getGUI() {
		// Class does not have its own GUI
		return null;
	}

	@Override
	public String getMergeClassName() {
		// No merge logic
		return null;
	}

	@Override
	public boolean iHaveMyOwnGUI() {
		// Class does not have its own GUI
		return false;
	}

}
